<?php
	get_header();
        $id = $_GET['version'];
        $value = get_post_meta($id, 'value_metadata', true);
        $saved = round(get_post_meta($id, 'save_metadata', true));
        $discount = get_post_meta($id,'discount_metadata',true);
        $value_after_discount = round(get_post_meta($id,'after_discount_metadata',true),2);
        $link_check_out = cpx_get_deal_mask_link($id);
        $link_check_out_text = get_post_meta($id, 'link_checkout_text_metadata', true);
        $ex = get_post_meta($id, 'extra_metadata', true);    
        $arr = cpx_filter_extra_fields($ex, (int)$discount);
        $jsons = array();
        $json['discount']= $discount;
        $json['save']= $saved;
        $json['value_after']= $value_after_discount;
        $json['value']= $value;
        $json['text']= $link_check_out_text;
        $json['mash_link']= $link_check_out;
        array_push($jsons, $json);
        foreach ($arr as $k=>$vs){
            $json['discount']=$vs['d'];
            $json['save']=$vs['s'];
            $json['value_after']=$vs['va'];
            $json['value']=$vs['v'];
            $json['text']=$vs['t'];
            $json['mash_link']=cpx_get_deal_mask_link($id, true, $k);
            array_push($jsons, $json);
        }
$response = json_encode($jsons);
?>
<div id="all-deals">
    <script type="text/javascript">
        function pageselectCallback(page_index, jq){
                // Get number of elements per pagionation page from form
                var result = <?php echo $response;?>;
                var items_per_page = 12;
                var max_elem = Math.min((page_index+1) * items_per_page, result.length);
                var newcontent = '';
                // Iterate through a selection of the content and build an HTML string
                for(var i=page_index*items_per_page;i<max_elem;i++)
                {
                    newcontent += '<ul class="list-version row-fluid"><li><div class="row-fluid">';
                    newcontent += '<div class="">';
                    newcontent +=                '<div class="version-discount">';
                    newcontent +=                    '<span class="tiny-price"><strong>Value</strong><br/><span class="price-num">$'+result[i]['value']+'</span></span>';
                    newcontent +=                    '<span class="tiny-price"><strong>Discount</strong><br/><span class="price-num">'+result[i]['discount']+'%</span></span>';
                    newcontent +=                    '<span class="tiny-price"><strong>You save</strong><br/><span class="price-num">$'+result[i]['save']+'</span></span>';
                    newcontent +=                '</div>';
                    newcontent +=              '</div>';
                    newcontent +=              '<div class="buy-content-button">';
                    newcontent +=                '<a class="buy-button1" href="'+result[i]['mash_link']+'">';
                    newcontent +=                    '<div class="green">';
                    newcontent +=                        '<span id="see-label">$'+result[i]['value_after']+'</span>';
                    newcontent +=                        '<span id="see-price-label">Buy</span>';
                    newcontent +=                    '</div>';
                    newcontent +=                '</a>';
                    newcontent +=              '</div>';
                    newcontent += '</div></li>';
                    newcontent += '<div class="row-fluid"><p class="version-name">'+result[i]['text']+'</p></div>';
                    newcontent +='</ul>';
                }
                // Replace old content with new content
                $('#Searchresult').html(newcontent);
                // Prevent click eventpropagation
                return false;
            }
            function getOptionsFromForm(){
                var opt = {callback: pageselectCallback};
                // Collect options from the text fields - the fields are named like their option counterparts
                opt = jQuery.extend({
                    items_per_page:12,
                    num_display_entries:5,
                    current_page:0,
                    num_edge_entries:2,
//                    link_to:"#current",
                    prev_text:"Prev",
                    next_text:"Next",
                    ellipse_text:"...",
                    prev_show_always:true,
                    next_show_always:true
            },opt||{});
                return opt;
            };
        jQuery(document).ready(function($) {
            var result = <?php echo $response;?>;
            var optInit = getOptionsFromForm();
            var items_per_page = 12;
            var page_index = result.length/items_per_page;
            //alert(Math.min(page_index));
            $(".pagination").pagination(result.length,optInit);
        });
    </script>
    
    <h1 id="inner-title">Choose your version in <?php echo count($arr)+1;?>&nbsp;versions</h1>
    <hr/>
    <?php 
        if($value !=''){ ?>
            <div id="Pagination" class="pagination row-fluid"></div>
            <div id="Searchresult">
                 <div>Results...</div>
            </div>
        <?php }
    ?>
</div>

<?php
	get_footer();
?>